/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.persistence.jdbc;

import apus.exception.DatabaseException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author 21vek
 */
public class DataBaseStatistics {

    public static Map<String, Integer> getStatistics() {
        Map<String, Integer> statistics = new HashMap<>();
        Connection connection = AbstractJdbcDAO.getConnection();
        Statement st = null;
        ResultSet rs = null;

        try {
            st = connection.createStatement();

            rs = st.executeQuery("SELECT COUNT(*) AS COU FROM SUBSCRIBER WHERE SUBSCRIBER_TYPE=0");
            if (rs.next()) {
                statistics.put("Persons", rs.getInt("COU"));
            }

            rs = st.executeQuery("SELECT COUNT(*) AS COU FROM SUBSCRIBER WHERE SUBSCRIBER_TYPE=1");
            if (rs.next()) {
                statistics.put("Organizations", rs.getInt("COU"));
            }

            rs = st.executeQuery("SELECT COUNT(*) AS COU FROM ACCOUNT");
            if (rs.next()) {
                statistics.put("Accounts", rs.getInt("COU"));
            }

            rs = st.executeQuery("SELECT COUNT(*) AS COU FROM PHONE_NUMBER");
            if (rs.next()) {
                statistics.put("Phone numbers", rs.getInt("COU"));
            }

            rs = st.executeQuery("SELECT COUNT(*) AS COU FROM PHONE_CALL");
            if (rs.next()) {
                statistics.put("Calls", rs.getInt("COU"));
            }

        } catch (SQLException ex) {
            throw new DatabaseException("Problems with DB");
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
            } catch (SQLException ex) {
                System.err.println("Can't close statement");
                ex.printStackTrace(System.err);
            }
        }
        return statistics;
    }
}
